package com.solocredit.finance.ui.steps.impl

import android.content.Context
import com.solocredit.finance.base.utils.log.MullaLog
import com.solocredit.finance.http.ApiService
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.mvp.presenter.MvpPresenter
import com.solocredit.finance.ui.steps.view.IMullaUploadView
import com.solocredit.finance.utils.okhttp.callback.StringCallback
import com.solocredit.finance.utils.okhttp.model.Progress
import com.solocredit.finance.utils.okhttp.model.Response
import com.solocredit.finance.utils.okhttp.request.base.Request
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/31 15:46
 * @Annotation：图片上传 Presenter 实现
 */
class MullaUploadPresenterImpl(context: Context?) : MvpPresenter<IMullaUploadView>(context) {

    /**
     * 上传图片
     */
    fun onMullaUploadImageAction(imgPath: String){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.MULLA_API_UPLOAD_FILE_URL)
        HttpHelper.uploadMullaFileAction(httpUrl,imgPath,object : StringCallback(){
            override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
                super.onStart(request)
                MullaLog.e("upload start")
            }
            override fun onSuccess(response: Response<String>?) {
                if (response?.body() == null){
                    return
                }
                MullaLog.e("upload success ："+ response.body())
                if (response.code() == 200){
                    mvpView?.hideMullaLoading()
                    val data = JSONObject(response.body())
                    when(data.optInt("code")){
                        0 -> {//成功
                            if (data.has("obj")){
                                val obj = data.optJSONObject("obj")
                                obj?.let {
                                    val imgUrl = it.optString("url")
                                    mvpView?.showMullaUploadImageSuccess(imgPath,imgUrl)
                                }
                            }
                        }
                        else -> {
                            val msg = data.optString("msg")
                            mvpView?.showMullaToast(msg)
                        }
                    }
                } else {
                    if (response.message() != null){
                        mvpView?.showMullaToast(response.message())
                    }
                }
            }
            override fun onError(response: Response<String>) {
                //是否是网络异常
                val errorCode = response.code()
                if (isNetworkError(errorCode)){
                    mvpView?.showFlexiMolaNetworkErrorDialog("Upload Image", onRetryClick = {
                        mvpView?.showMullaLoading()
                        onMullaUploadImageAction(imgPath)
                    })
                    return
                }
                if (response.message() != null){
                    mvpView?.showMullaToast(response.message())
                }
            }
            override fun uploadProgress(progress: Progress) {
               MullaLog.e("uploadProgress: $progress")
               MullaLog.e("uploadProgress，total: ${progress.totalSize}")
               MullaLog.e("uploadProgress，progress: ${progress.currentSize}")
            }
            override fun onFinish() {
                mvpView?.hideMullaLoading()
            }
        })
    }
}